﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web.UI;

namespace Channelvn.Cached.Base
{
    public class PageBase2 : Page
    {
        private static readonly Assembly _currentAssembly = Assembly.GetExecutingAssembly();
        CacheSettings.ControlSetting _controlSetting;

        protected override void OnPreLoad(EventArgs e)
        {
            CacheSettings settings = CacheSettings.GetCurrentSettings();

            // Read all settings ok
            if (settings.PageSettings.Length > 0)
            {
                CacheSettings.PageSetting pageSetting = settings.GetPageSetting(Request.FilePath);
                // Read page setting ok
                if (!string.IsNullOrEmpty(pageSetting.CacheName))
                {
                    base.EnableViewState = pageSetting.EnableViewState;
                    for (int i = 0; i < pageSetting.ControlSettings.Length; i++)
                    {
                        _controlSetting = pageSetting.ControlSettings[i];
                        Control ctrlContainer = FindControlRecursive((null != Master ? Master : (Control)this), _controlSetting.ContainerID);
                        if (ctrlContainer != null)
                        {
                            ControlBase ctrlControl = (ControlBase)LoadControl(_controlSetting.FilePath);
                            if (null != ctrlControl)
                            {
                                ctrlControl.EnableViewState = _controlSetting.EnableViewState;

                                if (null != _controlSetting.Properties)
                                {
                                    string[] keys = new string[_controlSetting.Properties.Keys.Count];
                                    _controlSetting.Properties.Keys.CopyTo(keys, 0);
                                    int propertyCount = keys.Length;
                                    for (int j = 0; j < propertyCount; j++)
                                    {
                                        ctrlControl.SetProperty(keys[j], _controlSetting.Properties[keys[j]]);
                                    }
                                }

                                if (!ctrlContainer.Controls.Contains(ctrlControl)) //ctrlContainer.Controls.Remove(ctrlControl);
                                    ctrlContainer.Controls.Add(ctrlControl);
                            }
                        }
                    }
                }
            }

            base.OnPreLoad(e);
        }

        private Control FindControlRecursive(Control root, string id)
        {
            if (root.ID == id) return root;

            for (int i = 0; i < root.Controls.Count; i++)
            {
                Control foundCtl = FindControlRecursive(root.Controls[i], id);

                if (foundCtl != null) return foundCtl;
            }
            return null;
        }
    }
}
